package net.yambal.android.geo;

import java.util.List;

import net.yambal.android.geo.db.LocationLogItem;
import net.yambal.android.util.StringUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ArrayAdapterLocationLogItem extends ArrayAdapter<LocationLogItem> {
	private int _textViewResourceId;
	private List<LocationLogItem> _locations;
	private LayoutInflater _inflater;
	private Boolean _is_show_search_key = false;
	
	public ArrayAdapterLocationLogItem(Context context, int textViewResourceId, List<LocationLogItem> logs, Boolean is_show_search_key) {
		super(context, textViewResourceId, logs);
		_textViewResourceId = textViewResourceId;
		_locations = logs;
		_is_show_search_key = is_show_search_key;
		_inflater = (LayoutInflater)context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE
		);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		if(convertView == null){
			convertView = _inflater.inflate(_textViewResourceId, null);
		}
		final LocationLogItem log = _locations.get(position);
		if(log != null){
			TextView mTitle = (TextView)convertView.findViewWithTag("textViewLocation");
			
			if(StringUtil.nullStrig(log.location_name, null)!=null && !_is_show_search_key){
				mTitle.setText("k:" + log.location_name + " " + Math.round(log.min_count / 60) + "h");
				
			}else if(StringUtil.nullStrig(log.address, null)!=null){
				mTitle.setText(log.address + " " + Math.round(log.min_count / 60) + "h");
				
			}else{
				mTitle.setText("?");
			}
			
			/*
			TextView mMinCount = (TextView)convertView.findViewWithTag("textViewMinCount");
			if(StringUtil.nullStrig(log.address, null)!=null){
				mMinCount.setText(Math.round(log.min_count / 60) + "h");
			}else{
				mMinCount.setText("?");
			}
			*/
		}
		return convertView;
	}
	
	@Override
    public LocationLogItem getItem(int position) {
      return _locations.get(position);
    }
	
	@Override
    public int getCount() {
      return _locations.size();
    }
	
	@Override
    public long getItemId(int position) {
      return position;
    }
}
